package com.cskaoyan.app.service.impl;

import com.cskaoyan.app.bean.po.*;
import com.cskaoyan.app.bean.vo.GoodsDetailVo;
import com.cskaoyan.app.mapper.*;
import com.cskaoyan.app.service.AdminGoodsDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.LinkedList;
import java.util.List;

@Service
public class AdminGoodsDetailServiceImpl implements AdminGoodsDetailService {
    @Autowired
    MarketGoodsMapper marketGoodsMapper;
    @Autowired
    MarketGoodsAttributeMapper marketGoodsAttributeMapper;
    @Autowired
    MarketGoodsProductMapper marketGoodsProductMapper;
    @Autowired
    MarketGoodsSpecificationMapper marketGoodsSpecificationMapper;
    @Autowired
    MarketCategoryMapper marketCategoryMapper;

    @Override
    public GoodsDetailVo getGoodsDetailVo(Integer id) {
        MarketGoods goods = marketGoodsMapper.selectByPrimaryKey(id);
        MarketGoodsProductExample example = new MarketGoodsProductExample();
        MarketGoodsProductExample.Criteria criteria = example.createCriteria();
        criteria.andGoodsIdEqualTo(id);
        List<MarketGoodsProduct> products = marketGoodsProductMapper.selectByExample(example);
        MarketGoodsSpecificationExample example1 = new MarketGoodsSpecificationExample();
        MarketGoodsSpecificationExample.Criteria criteria1 = example1.createCriteria();
        criteria1.andGoodsIdEqualTo(id);
        List<MarketGoodsSpecification> specifications = marketGoodsSpecificationMapper.selectByExample(example1);

        MarketGoodsAttributeExample example2 = new MarketGoodsAttributeExample();
        MarketGoodsAttributeExample.Criteria criteria2 = example2.createCriteria();
        criteria2.andGoodsIdEqualTo(id);
        List<MarketGoodsAttribute> attributes = marketGoodsAttributeMapper.selectByExample(example2);

        Integer id1 = goods.getCategoryId();
        LinkedList<Integer> categoryIds = null;
        if (id1 != 0) {

            MarketCategory category = marketCategoryMapper.selectByPrimaryKey(id1);
            Integer pid = category.getPid();
            categoryIds = new LinkedList<>();
            categoryIds.add(pid);
            categoryIds.add(id1);
        }
        GoodsDetailVo goodsDetailVo = new GoodsDetailVo();
        goodsDetailVo.setCategoryIds(categoryIds);
        goodsDetailVo.setAttributes(attributes);
        goodsDetailVo.setGoods(goods);
        goodsDetailVo.setProducts(products);
        goodsDetailVo.setSpecifications(specifications);
        return goodsDetailVo;

    }
}
